// datepicker
$(document).ready(function(){
    $(".datepicker").datepicker({
        showButtonPanel: true,
		dateFormat: "yy-mm-dd",
    	timeFormat: 'HH:mm:ss',
    	showSecond: true,
        changeMonth: true,
        changeYear: true,
        yearRange: "2010:2020",
        monthNamesShort: ['Tháng 1', 'Tháng 2', 'Tháng 3', 'Tháng 4', 'Tháng 5', 'Tháng 6', 'Tháng 7', 'Tháng 8', 'Tháng 9', 'Tháng 10', 'Tháng 11', 'Tháng 12']
    });
});
$(document).ready(function(){
    $(".datepicker2").datepicker({
        dateFormat: 'yy-mm-dd',
        changeMonth: true,
        changeYear: true,
        yearRange: "2010:2020",
        monthNamesShort: ['Tháng 1', 'Tháng 2', 'Tháng 3', 'Tháng 4', 'Tháng 5', 'Tháng 6', 'Tháng 7', 'Tháng 8', 'Tháng 9', 'Tháng 10', 'Tháng 11', 'Tháng 12']
    });
});
//tab
$(document).ready(function(){
    $('.tab-widget .tab-title li').click(function(){
        $('.tab-widget .tab-title li').removeClass('active');
        $(this).addClass('active');
        $('.tab-content').removeClass('active');
        var value = $(this).attr('index');
        $('.'+value).addClass('active');
    });
});
//mini menu
$(document).ready(function(){
    $('.navbar-menu ul li a').hover(function(){
        $(this).find('span.background').animate({'top':'0px'},100);
    },function(){
        $(this).find('span.background').animate({'top':'23px'},100);
    });
});
function getCookie(name){
    var re = new RegExp(name + "=([^;]+)");
    var value = re.exec(document.cookie);
    return (value != null) ? unescape(value[1]) : null;
}
function setCookie(c_name,value,exdays){
      var exdate=new Date();
      exdate.setDate(exdate.getDate() + exdays);
      var c_value=escape(value) + 
        ((exdays==null) ? "" : ("; expires="+exdate.toUTCString()));
      document.cookie=c_name + "=" + c_value;
}
//click sidebar - start
$(document).ready(function(){
    $("#menu-toggle").click(function(e) {
        e.preventDefault();
        $("#wrapper").toggleClass("active");
    });
    
    $('.sidebar-nav > li > span, .sidebar-nav > li > a').click(function(){
        var cookie=getCookie("checkMenu");
        if(cookie==0){
            setCookie('checkMenu','1','2000000');
        }
        else{
            setCookie('checkMenu','0','2000000');
        }
        $(this).parent().toggleClass('active');
        $(this).parent().find('ul').toggleClass('hidden');
    });
    //, 
    $('.sidebar-nav li').hover(function(){
        $(this).find('> a:first-child .background').animate({left:"0px"},"fast");
        $(this).find('> a:first-child .glyphicon').addClass('color-white');
        $(this).find('> span:first-child .background').animate({left:"0px"},"fast");
        $(this).find('> span:first-child .glyphicon').addClass('color-white');
    },function(){
        $(this).find('> a:first-child .background').animate({left:"-40px"},"fast");
        $(this).find('> a:first-child .glyphicon').removeClass('color-white');
        $(this).find('> span:first-child .background').animate({left:"-40px"},"fast");
        $(this).find('> span:first-child .glyphicon').removeClass('color-white');
    });
})
//click sidebar - end
//////////////////////////////////////////////////////////////////////////
// display chart or table
$(document).ready(function(){
    $('.widget-title .config span.glyphicon-calendar').click(function(){
        $('.widget-title .config span').removeClass('active');
        $(this).toggleClass('active');
        $(this).parent().find('.widget-calendar').toggleClass('hide');
        return false;
    });
    $('.widget-title .config span').not('.glyphicon-calendar').click(function(){
        $(this).parent().find('.widget-calendar').addClass('hide');
    });
    $('.widget-title .config .widget-calendar input[type="button"]').click(function(){
        var dateS = $(this).parent().find('input[name="dateStart"]').val();
        var dateE = $(this).parent().find('input[name="dateEnd"]').val();
        if (dateS==null || dateS=="")
        {   
            $(this).parent().find('.mess').addClass('bg-warning');
            $(this).parent().find('.mess').text('Vui lòng chọn ngày bắt đầu');
            $(this).parent().find('.mess').show();
            return false;
        }
        if (dateE==null || dateE=="")
        {
            $(this).parent().find('.mess').addClass('bg-warning');
            $(this).parent().find('.mess').text('Vui lòng chọn ngày kết thúc');
            $(this).parent().find('.mess').show();
            return false;
        }
        if (dateS>dateE)
        {
            $(this).parent().find('.mess').addClass('bg-warning');
            $(this).parent().find('.mess').text('Ngày bắt đầu không được lớn hơn ngày kết thúc');
            $(this).parent().find('.mess').show();
            return false;
        }
        var widgetId = $(this).parents('.widget').attr('index-id');
        var selecter = $(this).parents('.widget').find('.widget-content');
        $.ajax({
            url : "/widget/index/widgetId/"+widgetId,
            data: {
                dateStart : dateS,
                dateEnd : dateE
            },
            success: function(data){
                selecter.html(data);
            }
        });
        return false;
    });
    $('.widget-title .config span.glyphicon-th-large').click(function(){
        $(this).parent().parent().parent('.widget').find('.widget-content > .table').removeClass('hide');
        $(this).parent().parent().parent('.widget').find('.widget-content > .chart').addClass('hide');
        var widgetId = $(this).parent().parent().parent('.widget').attr('index-id');
        $.cookie('widgetIdDisplay'+widgetId,'table');
        $('.widget-title .config span').removeClass('active');
        $(this).addClass('active');
        return false;
    });
    $('.widget-title .config span.glyphicon-signal').click(function(){
        $(this).parent().parent().parent('.widget').find('.widget-content > .table').addClass('hide');
        $(this).parent().parent().parent('.widget').find('.widget-content > .chart').removeClass('hide');
        var widgetId = $(this).parent().parent().parent('.widget').attr('index-id');
        $.cookie('widgetIdDisplay'+widgetId,'chart');
        $('.widget-title .config span').removeClass('active');
        $(this).addClass('active');
        return false;
    });
    //edit or add new widget
    $('.widget-title .config span.glyphicon-pencil,.dashboard-menu span.icon.icon-add-widget').fancybox({
        type        : 'iframe',
		maxWidth	: 524,
		maxHeight	: 339,
		fitToView	: false,
		width		: '70%',
		height		: '70%',
		autoSize	: false,
		closeClick	: false,
		openEffect	: 'none',
		closeEffect	: 'none'
	});
    //remove widget
    $('.widget-title .config span.glyphicon-remove').click(function(){
        var r=confirm("Bạn thực sự muốn xóa");
        if (r==true){
            var widgetId = $(this).parent().parent().parent('.widget').attr('index-id');  
            var widget = $(this).parent().parent().parent('.widget');
            $.ajax({
                url : "/widget/delete",
                data: {
                    widgetId : widgetId
                },
                success: function(data){
                    widget.remove();
                }
            });
            return true;
        }else{
            return false;
        }        
    });
    //download file excel widget
    $('.widget-title .config span.glyphicon-download').click(function(){
        var dateS = $(this).parent().find('input[name=dateStart]').val();
        var dateE = $(this).parent().find('input[name=dateEnd]').val();
        var href = $(this).attr('href')+'/dateStart/'+dateS+'/dateEnd/'+dateE;
        window.open(href);
    });
});
//dashboard - start
$(document).ready(function(){
    $( "#dashboard-left, #dashboard-right" ).sortable({
        connectWith: ".connectedSortable",
        handle: ".widget-title .glyphicon-move",
        revert: 300,
        start: function (e,ui){ 
            $(ui.placeholder).slideUp();
        },
        change: function (e,ui){
            $(ui.placeholder).hide().slideDown();
        },
        //update: function( event, ui ) {alert("aaa")},
        beforeStop: function( event, ui ) {
            var dashboardLeft = $("#dashboard-left").sortable('toArray', {attribute: 'id'});
            //console.log(dashboardLeft);
            var dashboardRight = $("#dashboard-right").sortable('toArray', {attribute: 'id'});
            //console.log(dashboardRight);
            $.ajax({
                url : "/dashboard/index",
                data: {
                    act : 'update',                    
                    dashId : $('.dashboard').attr('id'),
                    dashLeft : dashboardLeft,
                    dashRight : dashboardRight
                },
                success: function(data){
                    //console.log(data);
                }
            });
        }
    });//.disableSelection()
});
//dashboard - end
//////////////////////////////////////////////////////////////////////////
//touch sidebar - start
$(document).ready(function(){
    var bodyWidth = $( window ).width();
    if(bodyWidth < 767){
        $("body").touchwipe({
             wipeRight: function() { 
                $('#wrapper').addClass('touch');
                //$('#menu-toggle').addClass('touch');
             },
             wipeLeft: function() { 
                $('#wrapper').removeClass('touch');
                //$('#menu-toggle').removeClass('touch');
             },
             min_move_x: 20,
             min_move_y: 20,
             preventDefaultEvents: true
        });
        $('.navbar-header').css('background-color','#D6D2CC');
    }
    $( window ).resize(function() {
        var bodyWidth = $( window ).width();
        if(bodyWidth < 767){
            $('#menu-toggle').removeClass('hidden');
            $("body").touchwipe({
                 wipeRight: function() { 
                    $('#wrapper').addClass('touch');
                    $('#menu-toggle').addClass('touch');
                 },
                 wipeLeft: function() { 
                    $('#wrapper').removeClass('touch');
                    $('#menu-toggle').removeClass('touch');
                 },
                 min_move_x: 20,
                 min_move_y: 20,
                 preventDefaultEvents: true
            });
            $('.navbar-header').css('background-color','#D6D2CC');
        }else{
            $('#menu-toggle').addClass('hidden');
        }
    });
});
//touch sidebar - end
            
///////////////////////////////
$('.navbar-menu ul li span.background.glyphicon-headphones').click(function(){
    harlemShake();
});
function harlemShake(){
    //javascript:(function(){function c(){var e=document.createElement("link");e.setAttribute("type","text/css");e.setAttribute("rel","stylesheet");e.setAttribute("href",f);e.setAttribute("class",l);document.body.appendChild(e)}function h(){var e=document.getElementsByClassName(l);for(var t=0;t<e.length;t++){document.body.removeChild(e[t])}}function p(){var e=document.createElement("div");e.setAttribute("class",a);document.body.appendChild(e);setTimeout(function(){document.body.removeChild(e)},100)}function d(e){return{height:e.offsetHeight,width:e.offsetWidth}}function v(i){var s=d(i);return s.height>e&&s.height<n&&s.width>t&&s.width<r}function m(e){var t=e;var n=0;while(!!t){n+=t.offsetTop;t=t.offsetParent}return n}function g(){var e=document.documentElement;if(!!window.innerWidth){return window.innerHeight}else if(e&&!isNaN(e.clientHeight)){return e.clientHeight}return 0}function y(){if(window.pageYOffset){return window.pageYOffset}return Math.max(document.documentElement.scrollTop,document.body.scrollTop)}function E(e){var t=m(e);return t>=w&&t<=b+w}function S(){var e=document.createElement("audio");e.setAttribute("class",l);e.src=i;e.loop=false;e.addEventListener("canplay",function(){setTimeout(function(){x(k)},500);setTimeout(function(){N();p();for(var e=0;e<O.length;e++){T(O[e])}},15500)},true);e.addEventListener("ended",function(){N();h()},true);e.innerHTML=" <p>If you are reading this, it is because your browser does not support the audio element. We recommend that you get a new browser.</p> <p>";document.body.appendChild(e);e.play()}function x(e){e.className+=" "+s+" "+o}function T(e){e.className+=" "+s+" "+u[Math.floor(Math.random()*u.length)]}function N(){var e=document.getElementsByClassName(s);var t=new RegExp("\\b"+s+"\\b");for(var n=0;n<e.length;){e[n].className=e[n].className.replace(t,"")}}var e=30;var t=30;var n=350;var r=350;var i="//s3.amazonaws.com/moovweb-marketing/playground/harlem-shake.mp3";var s="mw-harlem_shake_me";var o="im_first";var u=["im_drunk","im_baked","im_trippin","im_blown"];var a="mw-strobe_light";var f="//s3.amazonaws.com/moovweb-marketing/playground/harlem-shake-style.css";var l="mw_added_css";var b=g();var w=y();var C=document.getElementsByTagName("*");var k=null;for(var L=0;L<C.length;L++){var A=C[L];if(v(A)){if(E(A)){k=A;break}}}if(A===null){console.warn("Could not find a node of the right size. Please try a different page.");return}c();S();var O=[];for(var L=0;L<C.length;L++){var A=C[L];if(v(A)){O.push(A)}}})()r a="mw-strobe_light";var f="//s3.amazonaws.com/moovweb-marketing/playground/harlem-shake-style.css";var l="mw_added_css";var b=g();var w=y();var C=document.getElementsByTagName("*");var k=null;for(var L=0;L<C.length;L++){var A=C[L];if(v(A)){if(E(A)){k=A;break}}}if(A===null){console.warn("Could not find a node of the right size. Please try a different page.");return}c();S();var O=[];for(var L=0;L<C.length;L++){var A=C[L];if(v(A)){O.push(A)}}})()
}
//admin
$(document).ready(function(){
    $('.option-wrapper .fieldset .fieldset-content li label').click(function(){
        $(this).parent().find('input[type=checkbox]').click();
    });
    $('.option-wrapper .fieldset .legend label').click(function(){
        $(this).parent().find('input').click();
    });
    $('.option-wrapper .fieldset .legend input').click(function(){
        $(this).parent().parent().find("input").prop('checked', this.checked);
    });
    //add new dashboard
    
    $("#btnIcon").fancybox({
        'href'              : '/default/dashboard/icons',
		maxWidth	: 524,
		maxHeight	: 425,
        closeBtn    : false,
        'width'             : '80%',
        'height'            : '70%',
        'autoScale'         : true,
        'transitionIn'      : 'none',
        'transitionOut'     : 'none',
        'type'              : 'iframe',
        'afterClose'         : function(){
            var selectedIcon = localStorage.getItem("iconSelected");
            $("#icon").val(selectedIcon);
            $(".icon").attr('class',"icon glyphicon " +selectedIcon);
        }
    });
});




//